package com.liujit.bms.app.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujit.bms.app.domain.model.BlogArticleLabel;
import com.liujit.bms.app.domain.model.BlogLabel;
import com.liujit.bms.app.domain.result.BlogLabelResult;
import com.liujit.bms.app.mapper.BlogArticleLabelMapper;
import com.liujit.bms.app.service.BlogArticleLabelService;
import com.liujit.bms.app.service.BlogLabelService;
import com.liujit.common.constants.GlobalConstant;
import com.liujit.common.utils.DozerConvertor;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description 博客文章标签Service实现类
 * @Author liujun
 * @Date 2021-07-13 13:44:34
 */
@RequiredArgsConstructor
@Service
public class BlogArticleLabelServiceImpl extends ServiceImpl<BlogArticleLabelMapper, BlogArticleLabel>
        implements BlogArticleLabelService {

    private final BlogLabelService blogLabelService;

    @Override
    public List<Long> getLabelIdByArticleId(Long articleId) {
        LambdaQueryWrapper<BlogArticleLabel> wrapper = new LambdaQueryWrapper();
        wrapper.select(BlogArticleLabel::getLabelId);
        wrapper.eq(BlogArticleLabel::getArticleId, articleId);
        return super.listObjs(wrapper, item -> Convert.toLong(item));
    }

    @Override
    public List<BlogLabelResult> getLabelByArticleId(Long articleId) {
        List<Long> ids = this.getLabelIdByArticleId(articleId);
        if (CollectionUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        LambdaQueryWrapper<BlogLabel> wrapper = new LambdaQueryWrapper();
        wrapper.eq(BlogLabel::getEnabled, GlobalConstant.ENABLED_YES);
        wrapper.in(BlogLabel::getId, ids);
        List<BlogLabel> list = blogLabelService.list(wrapper);
        return DozerConvertor.convertor(list, BlogLabelResult.class);
    }
}
